package lab.dexter.bbsreader.managers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lab.dexter.bbsreader.ListBrowser;
import lab.dexter.bbsreader.ListManager;
import lab.dexter.bbsreader.NewSMTHHttpClient;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import lab.dexter.bbsreader.R;

public class SectionFocusManager extends ListManager {
	
	private final String TARGET_URL = "http://www.newsmth.net/atomic2.php?act=focus&sec=-1";
	private final String ROOT_URL = "http://www.newsmth.net/atomic2.php";
	
	private Pattern patternFocusSections;
	
	private class SectionItem{
		public String name;
		public String path;
		
		public SectionItem(String name_, String path_) {
			name = name_;
			path = path_;
		}

		@Override
		public String toString() {
			return name;
		}
	}
	
	private List<SectionItem> sections;

	public SectionFocusManager(Context context, Intent intent) {
		super(context, intent);
		patternFocusSections = Pattern.compile("<a href=\"(\\?act=focus\\&sec=[^>\"]+)\"[^>]*>([^<]+)</a>");
		sections = new ArrayList<SectionItem>();
	}

	@Override
	public int update() {
		// show some progress bar here
		try {
			NewSMTHHttpClient.getClient().fetchData(TARGET_URL, this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if(view == null) {
			LayoutInflater vi = (LayoutInflater)(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            view = vi.inflate(R.layout.section_focus, null);
		}
		SectionItem it = sections.get(position);
		if(it != null) {
			TextView tv = (TextView)view.findViewById(R.id.title);

			if (tv != null) {
				tv.setText(it.toString());
			}
		}
		return view;
	}

	public void dataProcessor(HttpEntity entity) {
		try {
			// stupid server massed up the charset header, so we do the decoding manually
			//content = EntityUtils.toString(entity);
			BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
			StringBuilder builder = new StringBuilder();
			String line = null;
			while((line = reader.readLine()) != null) {
				builder.append(line);
			}
			Matcher matcher = patternFocusSections.matcher(builder.toString());
			
			sections.clear();
			while(matcher.find()) {
				sections.add(new SectionItem(matcher.group(2), matcher.group(1)));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onItemClick(int position) {
		Intent intent = new Intent(mContext, ListBrowser.class);
		intent.setAction(Intent.ACTION_VIEW);
		intent.putExtra(ListBrowser.MGR, ListBrowser.MGR_FOCUS_TOPICS);
		intent.putExtra(FocusTopicsManager.PARAM_URL, ROOT_URL + sections.get(position).path);
		intent.putExtra(FocusTopicsManager.PARAM_SECTION, sections.get(position).name);
		mContext.startActivity(intent);
		return false;
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	public int getCount() {
		return sections.size();
	}

	public Object getItem(int position) {
		return sections.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return 0;
	}

	public int getViewTypeCount() {
		return 1;
	}
	
	@Override
	public String getTitleSuffix() {
		return "首页导读";
	}
}
